package net.magicalcloud.magicalcloud.model.modelImpl;

import net.magicalcloud.magicalcloud.entity.Files;
import net.magicalcloud.magicalcloud.entity.RestJson;
import net.magicalcloud.magicalcloud.entity.User;
import net.magicalcloud.magicalcloud.execption.CloudExecption;
import net.magicalcloud.magicalcloud.listener.CloudListener;
import net.magicalcloud.magicalcloud.model.AppURL;
import net.magicalcloud.magicalcloud.model.IFileModel;

import java.util.List;

import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static net.magicalcloud.magicalcloud.util.RetrofitUtil.getAppUrl;

import java.io.File;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static net.magicalcloud.magicalcloud.util.RetrofitUtil.getAppUrl;

/** 文件model接口实现
 * Created by Pei on 2018/6/7.
 */

public class FileModel implements IFileModel {
    @Override
    public void showRecycle(int userId, final CloudListener<List<Files>> listener) {
        //获取AppUrl
        AppURL url= getAppUrl();
        //定义call
        Call<RestJson<List<Files>>> call = url.showRecycle(userId);

        call.enqueue(new Callback<RestJson<List<Files>>>() {
            @Override
            public void onResponse(Call<RestJson<List<Files>>> call, Response<RestJson<List<Files>>> response) {
                if(response.body().getCode()==0){
                    //无异常，返回数据
                    listener.done(response.body().getData(),null);
                }else{
                    //构建异常
                    listener.done(null,new CloudExecption(response.body().getCode(),response.body().getMessage()));
                }
            }

            @Override
            public void onFailure(Call<RestJson<List<Files>>> call, Throwable t) {

            }
        });
    }

    @Override
    public void vagueSearchFile(String value, int userId, final CloudListener<List<Files>> listener) {
        //获取AppUrl
        AppURL url = getAppUrl();
        //定义call
        Call<RestJson<List<Files>>> call = url.vagueSearch(value, userId);

        call.enqueue(new Callback<RestJson<List<Files>>>() {
            @Override
            public void onResponse(Call<RestJson<List<Files>>> call, Response<RestJson<List<Files>>> response) {
                if (response.body().getCode() == 0) {
                    //无异常，返回数据
                    listener.done(response.body().getData(), null);
                } else {
                    //构建异常
                    listener.done(null, new CloudExecption(response.body().getCode(), response.body().getMessage()));
                }
            }

            @Override
            public void onFailure(Call<RestJson<List<Files>>> call, Throwable t) {
                //构建异常
                listener.done(null,new CloudExecption(t));
            }
        });
    }

    @Override
    public void searchSubfile(String id, final CloudListener<List<Files>> listener) {
        AppURL url =getAppUrl();

        Call<RestJson<List<Files>>> call=url.findSubfiles(id);
        call.enqueue(new Callback<RestJson<List<Files>>>() {
            @Override
            public void onResponse(Call<RestJson<List<Files>>> call, Response<RestJson<List<Files>>> response) {
                if(response.body().getCode()==0){
                    listener.done(response.body().getData(),null);
                }else{
                    listener.done(null,new CloudExecption(response.body().getCode(),response.body().getMessage()));
                }
            }

            @Override
            public void onFailure(Call<RestJson<List<Files>>> call, Throwable t) {
                listener.done(null,new CloudExecption(t));
            }
        });
    }

    @Override
    public void uploadFile(Map<String,String> options, Map<String, RequestBody> params, final CloudListener<Files> listener) {
        //获取AppUrl
        AppURL url= getAppUrl();
        //定义call
        Call<RestJson<Files>> call = url.uploadFiles(options,params);
        call.enqueue(new Callback<RestJson<Files>>(){

            @Override
            public void onResponse(Call<RestJson<Files>> call, Response<RestJson<Files>> response) {
                if(response.body().getCode()==0){
                    //无异常，返回数据
                    listener.done(response.body().getData(),null);
                }else{
                    //构建异常
                    listener.done(null,new CloudExecption(response.body().getCode(),response.body().getMessage()));
                }
            }

            @Override
            public void onFailure(Call<RestJson<Files>> call, Throwable t) {
                listener.done(null,new CloudExecption(t));
            }
        });
    }
}
